package com.crealth.handler;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.crealth.ConstantDefine;
import com.crealth.db.BankChannelDb;
import com.crealth.db.BankSpecialLimitDb;
import com.crealth.memcached.UserCached;
import com.crealth.model.BankChannelModel;
import com.crealth.model.BankSpecialLimitModel;
import com.crealth.model.UserModel;
import com.crealth.msg.NetMessageData;

public class BankChannelHandler {

	public static NetMessageData obtainAllBankChannel(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject resJson = new JSONObject();
		//JSONObject msgJson = revData.getRevDataMsgJson();
		/*
		int userID = msgJson.getIntValue("id");
		UserModel user = UserCached.selectUserByID(userID);
		if (user.getPosition() < ConstantDefine.Deputy_General_Manager) {
			resJson.put("resCode", ErrorCode.ERROR_Limited_Auth);
			sendMsg.setMsgBody(resJson.toJSONString());
			return sendMsg;
		}*/
		
		List<BankChannelModel> channells = BankChannelDb.selectAllBankChannel();
		JSONArray jsonArray = new JSONArray();
		for (BankChannelModel channel : channells) {
			jsonArray.add(channel.toJSONObject());
		}
		
		resJson.put("resCode", 0);
		resJson.put("bankChannels", jsonArray);
		sendMsg.setMsgBody(resJson.toJSONString());
		return sendMsg;
	}
	
	public static NetMessageData saveAllBankChannel(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject resJson = new JSONObject();
		JSONObject msgJson = revData.getRevDataMsgJson();
		int userID = msgJson.getIntValue("id");
		UserModel user = UserCached.selectUserByID(userID);
		if (user.getPosition() < ConstantDefine.Head_Manager) {
			resJson.put("resCode", ErrorCode.ERROR_Limited_Auth);
			sendMsg.setMsgBody(resJson.toJSONString());
			return sendMsg;
		}
		
		String bankChannels = msgJson.getString("bankChannels");
		JSONArray jsonArray = JSONArray.parseArray(bankChannels);
		if (jsonArray != null && jsonArray.size() > 0) {
			JSONArray resJsonArr = new JSONArray();
			for (int i = 0; i < jsonArray.size(); ++i) {
				JSONObject json = jsonArray.getJSONObject(i);
				BankChannelModel channel = new BankChannelModel();
				channel.parseFromJson(json);
				if (channel.getId() > 0) {
					BankChannelDb.updateBankChannelByID(channel);
					resJsonArr.add(channel.toJSONObject());
				}
				else {
					BankChannelDb.insertBankChannel(channel);
					if (channel.getId() > 0) {
						resJsonArr.add(channel.toJSONObject());
					}
				}
			}
			resJson.put("bankChannels", resJsonArr);
		}
		
		resJson.put("resCode", 0);
		sendMsg.setMsgBody(resJson.toJSONString());
		return sendMsg;
	}
	
	public static NetMessageData obtainAllBankSpecialLimit(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject resJson = new JSONObject();		
		
		List<BankSpecialLimitModel> specialLimitls = BankSpecialLimitDb.selectAllBankSpecialLimit();
		JSONArray jsonArray = new JSONArray();
		for (BankSpecialLimitModel spLimit : specialLimitls) {
			jsonArray.add(spLimit.toJSONObject());
		}
		
		resJson.put("resCode", 0);
		resJson.put("specialLimitls", jsonArray);
		sendMsg.setMsgBody(resJson.toJSONString());
		return sendMsg;
	}
	
	public static NetMessageData saveAllBankSpecialLimit(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject resJson = new JSONObject();
		JSONObject msgJson = revData.getRevDataMsgJson();
		int userID = msgJson.getIntValue("id");
		UserModel user = UserCached.selectUserByID(userID);
		if (user.getPosition() < ConstantDefine.Head_Manager) {
			resJson.put("resCode", ErrorCode.ERROR_Limited_Auth);
			sendMsg.setMsgBody(resJson.toJSONString());
			return sendMsg;
		}
		
		String specialLimitls = msgJson.getString("specialLimitls");
		JSONArray jsonArray = JSONArray.parseArray(specialLimitls);
		if (jsonArray != null && jsonArray.size() > 0) {
			JSONArray resJsonArr = new JSONArray();
			for (int i = 0; i < jsonArray.size(); ++i) {
				JSONObject json = jsonArray.getJSONObject(i);
				BankSpecialLimitModel spLimit = new BankSpecialLimitModel();
				spLimit.parseFromJson(json);
				if (spLimit.getId() > 0) {
					BankSpecialLimitDb.updateBankSpecialLimitByID(spLimit);
					resJsonArr.add(spLimit.toJSONObject());
				}
				else {
					BankSpecialLimitDb.insertBankSpecialLimit(spLimit);
					if (spLimit.getId() > 0) {
						resJsonArr.add(spLimit.toJSONObject());
					}
				}
			}
			resJson.put("specialLimitls", resJsonArr);
		}
		
		resJson.put("resCode", 0);
		sendMsg.setMsgBody(resJson.toJSONString());
		return sendMsg;
	}
	
}
